//
//  XXTGSearchAddressView.m
//  XiuXiuTuanGou
//
//  Created by 张敬 on 2019/4/4.
//  Copyright © 2019年 XiuXiuTuanGou. All rights reserved.
//

#import "XXTGSearchAddressView.h"

#define selfH [self fitSize:52]
#define cancelBtnW [self fitSize:44]


@interface XXTGSearchAddressView()<UITextFieldDelegate>

@property (nonatomic, strong) UIButton *cancelBtn; //取消按钮
@property (nonatomic, strong) UIView *leftView; //搜索图标
@property (nonatomic, strong) UIImageView *icon;

@property (nonatomic, strong) UITapGestureRecognizer *tap;


@end

@implementation XXTGSearchAddressView

- (instancetype)init
{
    self = [super initWithFrame:CGRectMake(0, 0, KScreenWidth, selfH)];
    
    if (self) {
        [self setupContent];
    }
    return self;
}


- (void)setupContent
{
    [self addSubview:self.textField];
    [self addSubview:self.cancelBtn];
}


#pragma mark - response methods

- (void)didClickedCancelButton
{
    [self cancelDealTextField];
}

//开始编辑
- (void)startDealTextField
{
    _isEditState = YES;
    
    _leftView.frame = CGRectMake(0, 0, 30, 30);
    _icon.mj_x = _leftView.width - _icon.width;
    
    
    
    //取消按钮
    _cancelBtn.hidden = NO;
    
    _textField.mj_w = KScreenWidth - [self fitSize:8] *2 - cancelBtnW;
    
    if (self.startBlock) {
        self.startBlock();
    }
}

//结束编辑
- (void)cancelDealTextField
{
    _isEditState = NO;
    _leftView.frame = CGRectMake(0, 0, (KScreenWidth - [self fitSize:8] *2)/2.0 -20, 30);
    _icon.mj_x = _leftView.width - _icon.width;
    
    
    //取消按钮
    _cancelBtn.hidden = YES;
    
    _textField.mj_w = KScreenWidth - [self fitSize:8] *2;
    _textField.text = @"";
    [_textField resignFirstResponder];
    
    if (self.endBlock) {
        self.endBlock();
    }
}

//MARK: - textField.delegate
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self startDealTextField];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//    [self cancelDealTextField];
    [_textField resignFirstResponder];
    return YES;
}

- (void)textFieldChanged:(UITextField *)textField
{
    
}

#pragma mark - setter and getter

- (UITextField *)textField
{
    if (!_textField)
    {
        _textField = [[UITextField alloc] init];
        _textField.size = CGSizeMake(KScreenWidth - [self fitSize:8] *2, selfH - [self fitSize:10]*2);
        _textField.backgroundColor = KWhiteColor;
        _textField.delegate = self;
        _textField.font = mFont([self fitSize:15]);
        _textField.placeholder = @"搜索地点";
        _textField.returnKeyType = UIReturnKeyDone;
        _textField.textColor = KRedColor;
        _textField.leftView = self.leftView;
        _textField.leftViewMode = UITextFieldViewModeAlways;
        //[_textField setValue:[NSNumber numberWithInt:30] forKey:@"paddingLeft"];//左
        [self addShadowAndCircleCorner:_textField.layer corner:5];
        
        [_textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

//搜索Icon
- (UIView *)leftView
{
    if (!_leftView) {
        
        _leftView = [[UIView alloc] init];
        _leftView.userInteractionEnabled = YES;
        _leftView.frame = CGRectMake(0, 0, (KScreenWidth - [self fitSize:8] *2)/2.0 -20 , 30);
        _icon = [[UIImageView alloc] init];
        _icon.image = [UIImage imageNamed:@"icon_search"];
        _icon.contentMode = UIViewContentModeCenter;
        _icon.userInteractionEnabled = YES;
        _icon.size = CGSizeMake(30, 30);
        _icon.mj_y = 0;
        _icon.mj_x = _leftView.width - _icon.width;
        
        [_leftView addSubview:_icon];
        
    }
    return _leftView;
}

//取消按钮
- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _cancelBtn.titleLabel.font = mFont([self fitSize:14]);
        _cancelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:KRedColor forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(didClickedCancelButton) forControlEvents:UIControlEventTouchUpInside];
        _cancelBtn.hidden = YES;
//        _cancelBtn.timeInterval = 0.5;
    }
    return _cancelBtn;
}


#pragma mark - other methods
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _textField.mj_x = [self fitSize:8];
    _textField.mj_y = [self fitSize:10];
    
    //取消按钮
    _cancelBtn.size = CGSizeMake(cancelBtnW, selfH - [self fitSize:10] *2);
    _cancelBtn.mj_x = KScreenWidth - _cancelBtn.width - [self fitSize:8];
    _cancelBtn.mj_y = [self fitSize:10];
    
}




@end
